# Create your views here.

from django.template import RequestContext 
from django.shortcuts import render_to_response, get_object_or_404
from l2ldjango.settings import DATABASES
from django.shortcuts import *
from django.contrib.auth.decorators import login_required
from l2ldjango.apps.usermanagement.models import *
from l2ldjango.apps.referral.models import *
from l2ldjango.apps.usermanagement.views import *

def runquery(qry):
    import MySQLdb   
    db = MySQLdb.connect("localhost",DATABASES['default']['USER'],DATABASES['default']['PASSWORD'],DATABASES['default']['NAME'] )
    cursor = db.cursor()
    cursor.execute(qry)  
    db.close()


    

def acceptReferral(request,referralID):
    r=Referral.objects.get(pk=referralID)
    r.case_no=random.randrange(9999999999)
    r.status='ACCEPTED'
    r.save()
    return HttpResponseRedirect('/l2l/users/notifications')
    
def declineReferral(request,referralID):
    r=Referral.objects.get(pk=referralID)    
    r.status='DECLINED'
    r.save()
    return HttpResponseRedirect('/l2l/users/notifications')

def viewReferrals(request):
    received_r=Referral.objects.filter(refered=request.user.id)
    sent_r=Referral.objects.filter(refered_by=request.user.id)
    variables = RequestContext(request,{'received_r':received_r,'sent_r':sent_r,'title':'Referrals','message':''})    
    return render_to_response('referral/view_referrals.html',variables)

def sendReferralMessageReceived(request,refID):
    r=Referral.objects.get(pk=refID)
    usr=User.objects.get(pk=r.refered_by.id)
    user_name='%s %s'%(usr.first_name,usr.last_name)
    username=' (%s)'%(usr.username)
    if request.method == 'POST':
        discussion=Discussion()
        discussion.sent_from=request.user
        discussion.sent_to=r.refered_by
        discussion.message=request.POST['message']
        discussion.save()                      
        qry='%s%s%s%s%s'%('insert into referral_referral_discussion(referral_id,discussion_id) values (',refID,',',discussion.id,')')
        runquery(qry)
        return HttpResponse('Replied Successfully')        
    else:
        variables = RequestContext(request,{'referralID':refID,'message':'','title':'Case Discussion with %s%s'%(user_name,username)})    
        return render_to_response('referral/send_referral_discussion.html',variables)   

def sendReferralMessageSender(request,refID):
    r=Referral.objects.get(pk=refID)
    usr=User.objects.get(pk=r.refered.id)
    user_name='%s %s'%(usr.first_name,usr.last_name)
    username=' (%s)'%(usr.username)
    if request.method == 'POST':
        discussion=Discussion()
        discussion.sent_from=request.user
        discussion.sent_to=r.refered
        discussion.message=request.POST['message']
        discussion.save()                      
        qry='%s%s%s%s%s'%('insert into referral_referral_discussion(referral_id,discussion_id) values (',refID,',',discussion.id,')')
        runquery(qry)
        return HttpResponse('Replied Successfully')        
    else:
        variables = RequestContext(request,{'referralID':refID,'message':'','title':'Case Discussion with %s%s'%(user_name,username)})    
        return render_to_response('referral/send_referral_discussion_sender.html',variables) 

def sendReferralTo(request,userID):
    usr=User.objects.get(pk=userID)
    user_name='%s %s'%(usr.first_name,usr.last_name)
    username=' (%s)'%(usr.username)
    if request.method == 'POST':
        r=Referral()
        r.refered=usr
        r.refered_by=request.user
        r.case=  request.POST['case'] 
        #status could be 'PENDING','APPROVED','DECLINED'
        r.status='PENDING'
        r.save() 
        return HttpResponse('Referral Sent Successfully.')
        #variables = RequestContext(request,{'userid':userID,'message':'','title':'%s%s%s'%('Refer Case to ',user_name,username)})    
        #return render_to_response('referral/send_referral.html',variables)    
    else:
        variables = RequestContext(request,{'userid':userID,'message':'','title':'%s%s%s'%('Refer Case to ',user_name,username)})    
        return render_to_response('referral/send_referral.html',variables)   
    
     